#include "start.h"
#include <QMutex>
StarT::StarT(QObject *parent) :
    QThread(parent)
{
}
void StarT::setB(AB<B> *tree)
{
    Partes=tree;
}
void StarT::setRN(RN *tree)
{
    Tarjet=tree;
}
void StarT::setYa(bool y)
{
    ya=y;
}
void StarT::setTipo(Persona *A)
{
    Tipo=A;
}
void StarT::setId(int id)
{
    idCaja=id;
}
void StarT::Mover(int id)
{
    switch (idCaja)
    {
    case 1:
        switch (id)
        {
        case 1:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(1,i,10);
                msleep(100);
            }
            for (int i=10; i<30; i+=10)
            {
                emit ImagMov(1,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(1,i,30);
                msleep(100);
            }
            break;
        case 2:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(2,i,60);
                msleep(100);
            }
            for (int i=60; i>30; i-=10)   //
            {
                emit ImagMov(2,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(2,i,30);
                msleep(100);
            }
            break;
        case 3:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(3,i,110);
                msleep(100);
            }
            for (int i=110; i>70; i-=10)  //i>30
            {
                emit ImagMov(3,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(3,i,30);
                msleep(100);
            }
            break;
        case 4:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(4,i,160);
                msleep(100);
            }
            for (int i=160; i>30; i-=10)
            {
                emit ImagMov(4,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(4,i,30);
                msleep(100);
            }
            break;
        case 5:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(5,i,210);
                msleep(100);
            }
            for (int i=210; i>30; i-=10)
            {
                emit ImagMov(5,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(5,i,30);
                msleep(100);
            }
            break;
        case 6:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(6,i,20);
                msleep(100);
            }
            for (int i=20; i<30; i+=10)
            {
                emit ImagMov(6,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(6,i,30);
                msleep(100);
            }
            break;
        case 7:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(7,i,70);
                msleep(100);
            }
            for (int i=70; i>30; i-=10)
            {
                emit ImagMov(7,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(7,i,30);
                msleep(100);
            }
            break;
        case 8:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(8,i,120);
                msleep(100);
            }
            for (int i=70; i>30; i-=10)  //i=120
            {
                emit ImagMov(8,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(8,i,30);
                msleep(100);
            }
            break;
        case 9:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(9,i,170);
                msleep(100);
            }
            for (int i=170; i>130; i-=10)  //i<30
            {
                emit ImagMov(9,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(9,i,30);
                msleep(100);
            }
            break;
        default:
            break;
        }
        break;
    case 2:
        switch (id)
        {
        case 1:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(1,i,10);
                msleep(100);
            }
            for (int i=10; i<120; i+=10)
            {
                emit ImagMov(1,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(1,i,120);
                msleep(100);
            }
            break;
        case 2:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(2,i,60);
                msleep(100);
            }
            for (int i=60; i<120; i+=10)  //i<120
            {
                emit ImagMov(2,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(2,i,120);
                msleep(100);
            }
            break;
        case 3:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(3,i,110);
                msleep(100);
            }
            for (int i=110; i<120; i+=10)
            {
                emit ImagMov(3,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(3,i,120);
                msleep(100);
            }
            break;
        case 4:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(4,i,160);
                msleep(100);
            }
            for (int i=160; i>120; i-=10)
            {
                emit ImagMov(4,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(4,i,120);
                msleep(100);
            }
            break;
        case 5:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(5,i,210);
                msleep(100);
            }
            for (int i=210; i>120; i-=10)
            {
                emit ImagMov(5,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(5,i,120);
                msleep(100);
            }
            break;
        case 6:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(6,i,20);
                msleep(100);
            }
            for (int i=20; i<120; i+=10)   //era +=10
            {
                emit ImagMov(6,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(6,i,120);
                msleep(100);
            }
            break;
        case 7:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(7,i,70);
                msleep(100);
            }
            for (int i=70; i<120; i+=10)  //i<120
            {
                emit ImagMov(7,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(7,i,120);
                msleep(100);
            }
            break;
        case 8:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(8,i,120);
                msleep(100);
            }
            for (int i=120; i<120; i+=10) //i<120
            {
                emit ImagMov(8,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(8,i,120);
                msleep(100);
            }
            break;
        case 9:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(9,i,170);
                msleep(100);
            }
            for (int i=170; i>120; i-=10)
            {
                emit ImagMov(9,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(9,i,120);
                msleep(100);
            }
            break;
        default:
            break;
        }
        break;
    case 3:
        switch (id)
        {
        case 1:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(1,i,10);
                msleep(100);
            }
            for (int i=10; i<210; i+=10)
            {
                emit ImagMov(1,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(1,i,210);
                msleep(100);
            }
            break;
        case 2:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(2,i,60);
                msleep(100);
            }
            for (int i=60; i<210; i+=10)
            {
                emit ImagMov(2,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(2,i,210);
                msleep(100);
            }
            break;
        case 3:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(3,i,110);
                msleep(100);
            }
            for (int i=110; i<210; i+=10)
            {
                emit ImagMov(3,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(3,i,210);
                msleep(100);
            }
            break;
        case 4:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(4,i,160);
                msleep(100);
            }
            for (int i=160; i<210; i+=10)
            {
                emit ImagMov(4,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(4,i,170);
                msleep(100);
            }
            break;
        case 5:
            for (int i=20; i<200; i+=10)
            {
                emit ImagMov(5,i,210);
                msleep(100);
            }
            for (int i=210; i<210; i-=10)
            {
                emit ImagMov(5,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(5,i,210);
                msleep(100);
            }
            break;
        case 6:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(6,i,20);
                msleep(100);
            }
            for (int i=20; i<210; i+=10)
            {
                emit ImagMov(6,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(6,i,20);
                msleep(100);
            }
            break;
        case 7:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(7,i,70);
                msleep(100);
            }
            for (int i=70; i<30; i-=10)   //i<210  //uno de los errores
            {
                emit ImagMov(7,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(7,i,210);
                msleep(100);
            }
            break;
        case 8:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(8,i,120);
                msleep(100);
            }
            for (int i=120; i<210; i+=10)  //i<210
            {
                emit ImagMov(8,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(8,i,210);
                msleep(100);
            }
            break;
        case 9:
            for (int i=40; i<200; i+=10)
            {
                emit ImagMov(9,i,170);
                msleep(100);
            }
            for (int i=170; i<210; i+=10)
            {
                emit ImagMov(9,190,i);
                msleep(100);
            }
            for (int i=190;i<390; i+=10)
            {
                emit ImagMov(9,i,210);
                msleep(100);
            }
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }
}

void StarT::run()
{
    //do
    Mover(Tipo->getid());

    QMutex mutex;
    mutex.lock();

    emit NamCh(idCaja,QString::fromStdString(Tipo->getNombre()));
    emit NunCh(idCaja, Tipo->getfx()->getTotal());
    emit TarCh(idCaja,QString::number(Tarjet->Index(Tipo->getCedula()).Num_Tarjeta));
    mutex.unlock();
    while(!ya)
        sleep(2);
    mutex.lock();
    /*
    for(int i =1; i<=Tipo->getfx()->GetPartes()->Largo(); i++)
    {
        Lista<unsigned int> temp = Tipo->getfx()->GetPartes();
        Partes->Borrar((temp)[i]->getValor());
    }
    */
    mutex.unlock();
    emit ImagDes(Tipo->getid());
    emit Finit(idCaja);
}
